package com.fk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fk.reggie.common.CustomException;
import com.fk.reggie.domain.Dish;
import com.fk.reggie.domain.Setmeal;
import com.fk.reggie.domain.SetmealDish;
import com.fk.reggie.dto.SetmealDto;
import com.fk.reggie.service.SetmealDishService;
import com.fk.reggie.service.SetmealService;
import com.fk.reggie.mapper.SetmealMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 凡凯
 * @description 针对表【setmeal(套餐)】的数据库操作Service实现
 * @createDate 2023-03-16 10:27:01
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
        implements SetmealService {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveSetmealWithDish(SetmealDto setmealDto) {
        //保存套餐
        setmealService.save(setmealDto);
        //给套餐下菜品添加 setmeal id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream()
                .peek(setmealDish -> setmealDish.setSetmealId(String.valueOf(setmealDto.getId())))
                .collect(Collectors.toList());
        //保存套餐下的菜品
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //删除套餐前  判断用户所选套餐是否已经停售
        //select count(*) from setmeal where id in (ids) and status =1
        //SELECT COUNT( * ) FROM setmeal WHERE is_deleted=0 AND (id IN (?) AND status = ?)
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId, ids)
                .eq(Setmeal::getStatus, 1);
        int count = setmealService.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不允许删除");
        }
        //删除套餐   逻辑删除
        setmealService.removeByIds(ids);
        //删除套餐下 菜品
        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(dishLambdaQueryWrapper);
    }

    @Override
    @Transactional
    public void updateStatus(Integer status, List<Long> ids) {
        //把名字为rhb的用户年龄更新为18，其他属性不变
        UpdateWrapper<Setmeal> setmealUpdateWrapper = new UpdateWrapper<>();
        setmealUpdateWrapper.set("status", status).in("id", ids);
        setmealService.update(setmealUpdateWrapper);
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //保存套餐更新
        setmealService.updateById(setmealDto);
        //删除套餐之前的菜品
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        //保存套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().peek(setmealDish -> setmealDish.setSetmealId(String.valueOf(setmealDto.getId()))).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
}




